home *** CD-ROM | disk | FTP | other *** search
- Path: beta.nedernet.nl!usenet
- From: jos@and.nl (Jos A. Horsmeier)
- Newsgroups: comp.lang.c,comp.lang.perl
- Subject: Re: Random File Access - I don't get it
- Date: 2 Apr 1996 13:02:04 GMT
- Organization: AND Operations Research B.V.
- Message-ID: <4jr8gc$l38@beta.nedernet.nl>
- References: <3160DE1E.495C@teleport.com>
- NNTP-Posting-Host: klepzeiker.and.nl
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <3160DE1E.495C@teleport.com>, kinards@teleport.com wrote:
-
- | As I was coding a simple program to randomly read lines from a text file
- |it occurred to me something was amiss. This is probably some fundamental
- |oversight on my part, but some illumination would be helpful...
- |
- | Suppose I have two files, one contains text (1 line of random length text
- |per 'record') and another which is my index into the text file, which contains
- |the starting and ending byte positions in the text file for each 'record'. Now
- |the question..
- |
- |Suppose the text file has 2,000,000 entries. Now, what's the difference in reading
- |1,000,000 lines from the index file to find the starting and ending byte positions
- |in the text file for record 1,000,000, and then seeking these in the text file,
- |rather than just reading 1,000,000 times from the text file to get the same 'record'
- |in the first place?
-
- The difference is, that there's (normally) no need to read the index file sequentially.
- Finding the appropriate entry in the index file can be as simple as:
-
- typedef struct {
- long offset;
- long end;
- } index_t;
-
- index_t idx;
-
- fseek(file, 1000000*sizeof(index_t), 0);
- fread(&index, sizeof(index_t), 1, file);
-
- If you _do_ have to read an index file sequentially (which is silly in most cases),
- still then there can be a slight speed gain if the data records happen to be very
- large, e.g. reading 1,000,000 index entries can be way faster than reading the same
- number of records of, say, 64Kb each.
-
- kind regards,
-
- Jos aka jos@and.nl
- --
- Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
-
-